package com.bcntouch.touch2act.Contribute;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bcntouch.touch2act.R;
import com.bcntouch.touch2act.Constants.Constants;
import com.bcntouch.touch2act.NFC.NFCUtils;

public class ContributionTagDetectedActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contribution_tag_detected);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_contribution_tag_detected, menu);
        return true;
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	
    	finish();
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	
    	Intent intent = getIntent();
    	String intentAction = intent.getAction();
    	
    	String contributionType = null;
    	
    	// NFC
    	if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intentAction)) {
//    		String tagUid = NFCUtils.getTagUidFromIntent(intent);
//    		Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    		List<Uri> uris = NFCUtils.getUris(intent);
    		contributionType = getContributionType(uris);
    	}
    	
    	// URI
    	else if (Intent.ACTION_VIEW.equals(intentAction)) {
    		Uri uri = intent.getData();
    		contributionType = getContributionType(uri);
    	}
    	
    	processContributionType(contributionType);
    	
    }
    
    public void processContributionType(String contributionType) {
    	if (contributionType == null) {
    		unknownContribution();
    		return;
    	}
    	
    	if (Constants.VALUE_CONTAINER.equals(contributionType)) {
    		String[] contributionOptions = {
        			getString(R.string.contenidor_trencat),
        			getString(R.string.contenidor_ple)
        			};
    		
    		fillView(getString(R.string.container), contributionOptions);
    	}
    	else if (Constants.VALUE_BULB.equals(contributionType)) {
    		String[] contributionOptions = {
        			getString(R.string.bombeta_fossa),
        			getString(R.string.intermitent)
        			};
    		
    		fillView(getString(R.string.farola), contributionOptions);
    	}
    	else {
    		unknownContribution();
    	}
    }
    
    public void fillView(final String headerText, final String[] contributionOptions) {
    	// Header Text
    	TextView headerTextView = (TextView) findViewById(R.id.header_text_view);
    	headerTextView.setText(headerText);
    	
    	// Fill the listView with the supplied data
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contributionOptions);
        ListView listView = (ListView) findViewById(R.id.contribution_options_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				onContributionOptionSelected(headerText, contributionOptions[position]);
			}
		});
    }
    
    public void unknownContribution() {
    	TextView headerTextView = (TextView) findViewById(R.id.header_text_view);
    	headerTextView.setText(R.string.unknown_contribution);
    }
    
    public void onContributionOptionSelected(String type, String text) {
    	// Save the contribution in DB
    	ContributionStorage.insertContribution(this, type, text);
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.aportacio_enviada)
    	       .setCancelable(false)
    	       .setPositiveButton(R.string.acceptar, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   Intent intent = new Intent(ContributionTagDetectedActivity.this, ContributeActivity.class);
    	        	   startActivity(intent);
    	        	   finish();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    public String getContributionType(List<Uri> uris) {
    	String contributionType = null;
    	
    	if (uris != null) {
			for (Uri uri: uris) {
				contributionType = getContributionType(uri);
			}
		}
    	
    	return contributionType;
    }
    
    public String getContributionType(Uri uri) {
    	if (uri == null)
    		return null;
    	
    	String contributionType = null;
    	
    	String incomingHost = uri.getHost();
    	if (Constants.APP_HOST.equals(incomingHost)) {
    		List<String> pathSegments = uri.getPathSegments();
    		if (pathSegments == null || pathSegments.isEmpty())
    			return null;
    		
    		String pathSegment = pathSegments.get(0);
    		if (Constants.PATH_CONTRIBUTION.equals(pathSegment)) {
    			contributionType = uri.getQueryParameter(Constants.PAR_CONTRIBUTION_TYPE);
    		}
    	}
    	
    	return contributionType;
    }
    
}
